#include "storeinfo.h"
#include "ui_storeinfo.h"

StoreInfo::StoreInfo(QWidget *parent,int sid) :
    QDialog(parent),
    ui(new Ui::StoreInfo)
{
    ui->setupUi(this);
    this->myDatabase.getConnection();
    m_sid = sid;


    QSqlQuery sql;

    sql.prepare("SELECT * FROM tb_store WHERE sid = :sid");
    sql.bindValue(":sid", sid);
    sql.exec();
    QString storeAccount;
    QString storePassword;
    QString storeName;
    QDate storeinDate;
    QString storetele;
    int storelevel;
    float storeprofit;
    bool storevip;
    QString storeAddress;
    while(sql.next()){
        if(sid == sql.value("sid").toInt()){
            storeAccount = sql.value("saccount").toString();
            storePassword = sql.value("spassword").toString();
            storeName = sql.value("sname").toString();
            storeinDate = sql.value("sindate").toDate();
            storetele = sql.value("stele").toString();
            storelevel = sql.value("slevel").toInt();
            storeprofit = sql.value("sprofit").toFloat();
            storeAddress = sql.value("saddress").toString();
        }
    }


    ui->Edit_sid->setText(QString::number(m_sid));
    ui->Edit_account->setText(storeAccount);
    ui->Edit_password->setText(storePassword);
    ui->Edit_sname->setText(storeName);
    ui->Edit_indate->setText(storeinDate.toString());
    ui->Edit_level->setText(QString::number(storelevel));
    ui->Edit_stele->setText(storetele);
    ui->Edit_profit->setText(QString::number(storeprofit));
    ui->Edit_address->setText(storeAddress);



    sql.prepare("SELECT * FROM tb_store_vip WHERE sid = :sid");
    sql.bindValue(":sid", sid);
    sql.exec();
    while(sql.next()){
        if(sid == sql.value("sid").toInt()){
            storevip = sql.value("svip").toBool();
        }
    }
    if(storevip)
        ui->svip->setText("VIP");
}

StoreInfo::~StoreInfo()
{
    delete ui;
}

void StoreInfo::on_Ensure_clicked()
{
    QSqlQuery sql;
    QString storePassword = ui->Edit_password->text();
    sql.prepare("UPDATE tb_store SET spassword = :storePassword WHERE sid = :sid");
    sql.bindValue(":storePassword", storePassword);
    sql.bindValue(":sid", m_sid);
    sql.exec();
\
    QString storeName = ui->Edit_sname->text();
    sql.prepare("UPDATE tb_store SET sname = :storeName WHERE sid = :sid");
    sql.bindValue(":storeName", storeName);
    sql.bindValue(":sid", m_sid);
    sql.exec();

    QString storeTele = ui->Edit_stele->text();
    sql.prepare("UPDATE tb_store SET stele = :storeTele WHERE sid = :sid");
    sql.bindValue(":storeTele", storeTele);
    sql.bindValue(":sid", m_sid);
    sql.exec();

    QString storeAddress = ui->Edit_address->text();
    sql.prepare("UPDATE tb_store SET saddress = :storeAddress WHERE sid = :sid");
    sql.bindValue(":storeAddress", storeAddress);
    sql.bindValue(":sid", m_sid);
    sql.exec();
    QMessageBox::information(this,"提示","信息更新成功！",QMessageBox::Yes);
}

